from PyQt5 import QtWidgets
from DBInit.DBInit import DBInit
from LoginUI import Ui_LoginUI
from SalaryMain import SalaryMain
import sys
import sqlite3
import os

class LoginWindow(QtWidgets.QWidget,Ui_LoginUI):
    def __init__(self,dbpath='test.db'):
        
        super(LoginWindow,self).__init__()
        self.setupUi(self)
        #Logic of ui
        self.LoginButton.clicked.connect(self.Login)
        self.RegisterButton.clicked.connect(self.Register)
        
        self.dbpath=dbpath #path of database file
        self.username = ''
        self.MainWindow = SalaryMain(self.username,self.dbpath)
        
    def Login(self):
        #get the input information
        username=self.UsernameEdit.text()
        password=self.PasswordEdit.text()
        #get the users information from database
        conn=sqlite3.connect(self.dbpath)
        c=conn.cursor()
        c.execute("SELECT * FROM users WHERE name=?",[username])
        db_user=c.fetchall()
        if db_user==[]:
            QtWidgets.QMessageBox.information(self,'Warning:',"Username:%s does not exist!"%username)
            return
        else:
            db_password=db_user[0][2]
            if password==db_password:
                self.username = username
                self.close()

                #login sucessed ,return username,show mainwindow
                self.MainWindow.__init__(self.username,self.dbpath)
                self.MainWindow.show()
                #self.MainWindow.setWindowTitle("Welcome to %s's Salary Notebook "%self.username)
                #self.MainWindow.username = username
                
                return     
            else:
                QtWidgets.QMessageBox.information(self,'Warning:',"Check your password!")
                return
        c.close()
        conn.close()
         
    def Register(self):
        #get the input information
        username = self.UsernameEdit.text()
        password = self.PasswordEdit.text()
        #check db users
        conn = sqlite3.connect(self.dbpath)
        c = conn.cursor()
        c.execute("SELECT * FROM users WHERE name=?",[username])
        db_user = c.fetchall()
        if username == '':
            QtWidgets.QMessageBox.information(self,'Reminding:',"Username can not be empty!")
        else:
            if not db_user == []:
                QtWidgets.QMessageBox.information(self,'Reminding:',"Username:%s is used,please input a new name.\n!"%username)
            else:
                QtWidgets.QMessageBox.information(self,'Reminding:',"Regist sucessed!")
                #regist sucessed ,insert users information into db.users
                c.execute("INSERT INTO users(name,password) VALUES (?,?)",(username,password))
                conn.commit()
                return
        c.close()
        conn.close()



if __name__ == "__main__":
    #init database
    dbpath = 'salary.db'
    DBInit(dbpath)
    # login
    app = QtWidgets.QApplication(sys.argv)
    appwindow = LoginWindow(dbpath)
    appwindow.show()
    sys.exit(app.exec())
        
        
